import { Component, OnInit } from '@angular/core';
import {Order} from '../../../model/order';
import {Part} from '../../../model/part';
import {ExtPart} from '../../../model/ext-part';
import {Guid} from '../../../model/guid';
import {Customer} from '../../../model/customer';
import {OrderService} from '../order.service';

@Component({
  selector: 'app-order-edit',
  templateUrl: './order-edit.component.html',
  styleUrls: ['./order-edit.component.css']
})
export class OrderEditComponent implements OnInit {

  order: Order = new Order();
  part: Part = new Part();
  extPart: ExtPart = new ExtPart();
  tempId = Guid.newGuid();
  extPartList: Array<ExtPart> = [];
  partList: Array<Part> = [];
  updateIndex = -1;
  customerList: Array<Customer> = [];
  config: {};

  addExtPart(extpart: ExtPart): void { // 添加增配项目
    extpart.partId = this.tempId;
    extpart.extpartId = Guid.newGuid();
    this.extPartList.push(extpart);
    this.extPart = new ExtPart();
  }
  addPart(part: Part): void { // 添加标配项目
    part.partId = this.tempId;
    part.extPartList = this.extPartList;
    part.partTotalPrice = this.getPartTotalPrice;
    part.partTotalSquare = this.getPartTotalSquare;
    this.partList.push(part);
    this.extPartList = [];
    this.part = new Part();
    this.tempId = Guid.newGuid();
  }

  deleteExtPart(index) { // 删除增配项目
    this.extPartList.splice(index, 1);
  }
  deletePart(index) { // 删除标配项目
    this.partList.splice(index, 1);
  }
  editPart(part: Part, i: number) {
    console.log(i);
    this.updateIndex = i;
    this.part = part;
    this.extPartList = part.extPartList;
  }
  doEditPart() {
    this.part.partTotalPrice = this.getPartTotalPrice;
    this.part.partTotalSquare = this.getPartTotalSquare;
    this.partList.splice(this.updateIndex, 1, this.part);
    this.part = new Part();
    this.extPartList = [];
    this.updateIndex = -1;
  }
  onSubmit(order: Order): void { // 提交
    order.partList = this.partList;

  }
  get getPartTotalPriceExcExtPart(){
    return this.getPartTotalSquare * this.part.partPrice * this.part.partDiscount;
  }
  get getPartTotalPrice() { // 获取当前标配项目总价（含增配价格）
    return this.getPartTotalPriceExcExtPart + this.extPartTotalPrice - this.part.partMinusDiscount;
  }
  get getPartTotalSquare() {
    return (this.part.partHeight * this.part.partWidth) / 1000000 + this.part.partExtend;
  }
  get extPartTotalPrice() { // 获取当前增配项目总价
    let total = 0;
    this.extPartList.forEach(function (value, index, array) {
      total += array[index].extpartAmount * array[index].extpartQuantity;
    });
    return total;
  }
  get totalPrice() { // 获取订单总价
    let total = 0;
    this.partList.forEach(function (value, index, array) {
      total += array[index].partTotalPrice;
    });
    return total;
  }
  get totalSquare() { // 获取订单总面积
    let total = 0;
    this.partList.forEach(function (value, index, array) {
      total += array[index].partTotalSquare;
    });
    return total;
  }
  get extPartNameList() { // 获取表格增配字符串
    let str = '';
    this.partList.forEach(function (value, index, array) {
      array[index].extPartList.forEach(function (value1, index1, array1) {
        str += array1[index1].extpartName + ' ' + array1[index1].extpartQuantity + ' * ' + array1[index1].extpartAmount + '\n';
      });
    });
    return str;
  }
  changeCustomerInf() {
    for (let i = 0; i < this.customerList.length; i++) {
      if (this.order.customerId == this.customerList[i].customerId) {
        this.order.customerName = this.customerList[i].customerName;
        this.order.customerAddress = this.customerList[i].customerAddress;
        this.order.customerTel = this.customerList[i].customerTel;
      }
    }
  }

  constructor(private orderService: OrderService) { }

  ngOnInit() {
    // TODO 初始化编辑订单页面
  }

}
